package Or

import (
	"context"
	"fmt"
	"db2s/global"
	"db2s/parDef"
	"db2s/ref"
	"math"
)

func (sum SumF) Rows(ctx context.Context) (result global.Return, err error) {
	var event = "[Rows]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var _, table = parDef.GetExecFuncParameterValueBase(ctx)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		if result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, tableSysObjectView, getFullRowsSizeSql, QueryColValManage)); err != nil {
			return
		}
		return parDef.ResultExec(parDef.NewGetExternalExecFuncParameterValue(ctx, event, result, result.Result, table))
	}
}
func (sum SumF) Count(ctx context.Context) (result global.Return, err error) {
	var event = "[Count]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var _, table = parDef.GetExecFuncParameterValueBase(ctx)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		if result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, tableSysObjectView, func(o string, s string, t string) string {
			return fmt.Sprintf("select /*+ parallel(5) */ "+
				"count(1) as \"tableRows\" "+
				"from %v %v T \n"+
				"where 1=1 %v %v %v",
				tableObject(parDef.GetExecFuncParameterValueBaseP(ctx)),
				specifyScn(parDef.GetParameterValueAnyToString(parDef.GetFuncParameterValue(ctx, parDef.GetTablePositParameterValue()))),
				rowsLimitC(parDef.GetParameterValueAnyToInt(parDef.GetFuncParameterValue(ctx, parDef.GetTableLimitParameterValue()))),
				whereSqlFilter(parDef.GetParameterValueAnyToString(parDef.GetFuncParameterValue(ctx, parDef.GetTableWhereSqlParameterValue()))),
				whereAddFilter(parDef.GetParameterValueAnyToMapStringGarth(parDef.GetFuncParameterValue(ctx, parDef.GetTableWhereAddParameterValue())), parDef.GetParameterValueAnyToColMetaMapSGarth(parDef.GetFuncParameterValue(ctx, parDef.GetTableColumnMapParameterValue()))))
		}, QueryColValManage)); err != nil {
			return
		}
		return parDef.ResultExec(parDef.NewGetExternalExecFuncParameterValue(ctx, event, result, result.Result, table))
	}
}
func (sum SumF) Sum(ctx context.Context) (result global.Return, err error) {
	var sumValue int64
	var event = "[Sum]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		if result, err = sum.Rows(ctx); err != nil {
			err = ref.ErrAddPrintf(event, err)
			return
		}
		sumValue = result.Result.(int64)
		if sumValue > 1000000 {
			return
		}
		if result, err = sum.Count(ctx); err != nil {
			err = ref.ErrAddPrintf(event, err)
		}
	}
	return
}
func (sum SumF) DataSizeMB(ctx context.Context) (result global.Return, err error) {
	var event = "[DataSizeMB]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var _, table = parDef.GetExecFuncParameterValueBase(ctx)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		if result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, objectSysObjectView, getFullDataSizeMBSql, QueryColValManage)); err != nil {
			return
		}
		return parDef.ResultExec(parDef.NewGetExternalExecFuncParameterValue(ctx, event, result, result.Result, table))
	}
}
func (sum SumF) IndexSizeMB(ctx context.Context) (result global.Return, err error) {
	var event = "[IndexSizeMB]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var _, table = parDef.GetExecFuncParameterValueBase(ctx)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		if result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, indexCategorySysObjectView, getFullIndexSizeMBSql, QueryColValManage)); err != nil {
			return
		}
		return parDef.ResultExec(parDef.NewGetExternalExecFuncParameterValue(ctx, event, result, result.Result, table))
	}
}
func (sum SumF) DataLength(ctx context.Context) (result global.Return, err error) {
	var dataSize, indexSize float64
	var event = "[DataLength]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		if result, err = sum.DataSizeMB(ctx); err != nil {
			err = ref.ErrAddPrintf("DataLength", err)
			return
		}
		dataSize = result.Result.(float64)
		if result, err = sum.IndexSizeMB(ctx); err != nil {
			err = ref.ErrAddPrintf("DataLength", err)
			return
		}
		indexSize = result.Result.(float64)
		result.Result = math.Round((dataSize+indexSize)*100) / 100
	}
	return
}
func (sum SumF) AvgRowsSize(ctx context.Context) (result global.Return, err error) {
	var event = "[AvgRowsSize]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var _, table = parDef.GetExecFuncParameterValueBase(ctx)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		if result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, tableStatisticsSysObjectView, getFullAvgRowsSizeSql, QueryColValManage)); err != nil {
			return
		}
		return parDef.ResultExec(parDef.NewGetExternalExecFuncParameterValue(ctx, event, result, result.Result, table))
	}
}
